package edu.humc.controller;

import edu.humc.pojo.MessageTable;
import edu.humc.service.Impl.MessageTableServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller("MessageTableController")
public class MessageTableController {
    @Resource(name = "MessageTableServiceImpl")
    MessageTableServiceImpl messageTableService;
    @RequestMapping(value = "/AddMessage",method = RequestMethod.POST)
    public String AddMessage(MessageTable messageTable, Model model ,HttpServletRequest request ){
//        获取留言发表日期
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        messageTable.setMdate(formatter.format(date));
//        留言id生成
        Random rand=new Random();
        messageTable.setMid((new SimpleDateFormat("yyyyMMdd")).format(date).toString()+((rand.nextInt(900)+100)+""));
//        获取留言人
        int id=(int)request.getSession().getAttribute("id");
        messageTable.setId(id);
        if (messageTableService.AddMessage(messageTable)) {
//            返回值为index.jsp会出现页面套页面的情况
//            return "/index.jsp";
            return "/AddMessage.jsp";
        }
        model.addAttribute("errorMessage","留言错误");
        return "/error.jsp";
    }
//    查询留言controller
//    存在bug：点击管理留言按钮后跳转到新的页面，而非返回index嵌套的iframe中去
    @RequestMapping("/SelectMessage")
    public String SelectMessage(int id,HttpSession httpSession){
        List<MessageTable> MessageTableList =messageTableService.SelectMessage(id);
        httpSession.setAttribute("MessageTableList",MessageTableList);
        return "/MessageList.jsp";
    }
//    删除留言
    @RequestMapping("/DeleteMessage")
    public String DeleteMessage(String mid,Model model){
//        添加进入回收站存在问题
        System.out.println(mid);
//        1、通过留言id查找留言
        MessageTable messageTable=messageTableService.DisplayDetail(mid);
        System.out.println(messageTable);
//        2、添加留言进入回收站
        messageTableService.AddRecycle(messageTable);
//        3、删除留言
        if (messageTableService.DeleteMessage(mid)){
//            返回值为index.jsp页面会出现iframe层层嵌套的问题，返回messagelist.jsp页面存在删除后需要刷新后才可以显示删除结果
//            return "/index.jsp";
            return "/MessageList.jsp";
        }
        model.addAttribute("OpeateErrorMessage","删除留言错误");
        return "/OpeateErrorPage.jsp";
    }
    @RequestMapping("/MessageOpeate")
    public String MessageOpeate(String mid,String act){
            return "/DiaplayDetail";
    }
    @RequestMapping("/DiaplayDetail")
    public String DiaplayDetail(String mid, String act, HttpSession session){
        MessageTable messageTable=messageTableService.DisplayDetail(mid);
        session.setAttribute("message",messageTable);
        if (act.equals("1")){
            session.setAttribute("mid",mid);
            return "/MessageUpdate.jsp";
        }
        return "/MessageDetail.jsp";
    }
    //修改留言
    @RequestMapping(value = "/MessageUpdate",method = RequestMethod.POST)
    public String MessageUpdate(String mtitle,String mcontent,HttpSession session,Model model){
        MessageTable messageTable=new MessageTable();
        String mid=(String) session.getAttribute("mid");
        messageTable.setMid(mid);
        messageTable.setMcontent(mcontent);
        messageTable.setMtitle(mtitle);
        System.out.println(messageTable);
        if (messageTableService.UpdateMessage(messageTable)) {
            return "/MessageUpdate.jsp";
        }
        model.addAttribute("OpeateErrorMessage","更新留言错误！");
        return "/OpeateErrorPage.jsp";
    }
}
